<?php
session_start();

include_once("../lib/functions.php");
include_once("../smiley.php");
getProjectRootPath();
//die("DOC-ROOT: ".$_SERVER['DOCUMENT_ROOT']);
require_once($_SERVER['DOCUMENT_ROOT']."/updates/PostActions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/updates/ReadNewsFeed.php");

$actionType=0;
if(isset($_POST["actiontype"]) && is_numeric(trim($_POST["actiontype"])) && trim($_POST["actiontype"])>0){
    //mean there is some action to perform
    $actionType=trim($_POST["actiontype"]);
    $feedid=0;
    $postAction=null;
    $postGenricTask=null;
    $postedContent = "";
    $privSettings="";
    $postTypeRead="";
    $loggedInId=0;
    $posttypeId=0;
    $postparentid=0;
    $clusteredId=0;
    $commentId=0;
    try {
            $oLog= new Log();
            $postAction = new PostAction();
            $postGenricTask= new PerformPostAction();
            $postedContent = "";
            $privSettings = PrivacySettings::DefaultSetting;
            $postTypeRead=PostType::Status_Updates;
            $postcommentMsg="";
            $taggedUsersArr=array();
        //try to read all psted values
            if (isset($_SESSION["Allyfolio_User_ID"]) && !empty($_SESSION["Allyfolio_User_ID"])) {
                $loggedInId = trim($_SESSION["Allyfolio_User_ID"]);
            }
            if (isset($_POST["feedid"]) && !empty($_POST["feedid"])) {
                $feedid = trim($_POST["feedid"]);
            }
            if (isset($_POST["commentid"]) && !empty($_POST["commentid"])) {
                $commentId = trim($_POST["commentid"]);
            }
            
            if (isset($_POST["feedcomment"]) && !empty($_POST["feedcomment"])) {
                $postcommentMsg = trim($_POST["feedcomment"]);
            }
            if (isset($_POST["feedtypeid"]) && !empty($_POST["feedtypeid"])) {
                $posttypeId = trim($_POST["feedtypeid"]);
            }
            if (isset($_POST["feedparentid"]) && !empty($_POST["feedparentid"])) {
                $postparentid = trim($_POST["feedparentid"]);
            }
            if(isset($_POST["taggedusers"]) && is_array($_POST["taggedusers"]) && count($_POST["taggedusers"])>0){
                $taggedUsersArr=$_POST["taggedusers"];
            }
                        
        if($loggedInId >0 && $feedid >0){            
            switch($actionType){
                case 1: //like                
                    try {
                        $taskResult= $postGenricTask->likePost($feedid,$loggedInId);
                        switch ($taskResult["err_code"]) {
                            case 200:
                                $objReadNewsFeed= new ReadFeedAction();
                                $totalLikescount=$objReadNewsFeed->getPostLikesCount($feedid);
                                echo $totalLikescount;
                                break;
                            default:
                                echo $taskResult["msg"];
                                break;
                        }
                    }
                    catch(Exception $ex){
                        $oLog->writeException('generictask: likepost():: \t'. $ex->getMessage(). "\n");
                        echo "400";
                    }
                    break;
               case 5: //like - comment                
                    try {
                        $taskResult= $postGenricTask->likeComment($feedid,$commentId,$loggedInId);
                        switch ($taskResult["err_code"]) {
                            case 200:
                                $objReadNewsFeed= new ReadFeedAction();
                                $totalLikescount=$objReadNewsFeed->getCommentLikesCount($feedid,$commentId);
                                echo $totalLikescount;
                                break;
                            default:
                                echo $taskResult["msg"];
                                break;
                        }
                    }
                    catch(Exception $ex){
                        $oLog->writeException('generictask: likepost():: \t'. $ex->getMessage(). "\n");
                        echo "400";
                    }
                    break;
                case 3: // update post privacy
                    try {
                        $taskResult= $postGenricTask->likePost($feedid,$loggedInId);
                        switch ($taskResult["err_code"]) {
                            case 200:
                                $objReadNewsFeed= new ReadFeedAction();
                                $totalLikescount=$objReadNewsFeed->getPostLikesCount($feedid);
                                echo $totalLikescount;
                                break;
                            default:
                                echo $taskResult["msg"];
                                break;
                        
                        }
                    }
                    catch(Exception $ex){
                        $oLog->writeException('generictask: likepost():: \t'. $ex->getMessage(). "\n");
                        echo "400";
                    }
                    break;    
                case 4: //comment
                    try {
                        $imgSrcArr=array();
                        if(isset($_POST["uploadimgsrcs"]) && is_array($_POST["uploadimgsrcs"]) && count($_POST["uploadimgsrcs"])>0){
                            $imgSrcArr=$_POST["uploadimgsrcs"];
                        }
                        $taskResult= $postGenricTask->commentPost($feedid,$loggedInId,$postcommentMsg,$taggedUsersArr,$imgSrcArr);
                        switch ($taskResult["err_code"]) {
                            case 200:
                                $objReadNewsFeed= new ReadFeedAction();
                                $postComments=$objReadNewsFeed->ReadPostComments($feedid);
                                getPostCommentsHtml($postComments,$feedid);
                                //echo $taskResult["msg"];
                                break;
                            default:
                                echo $taskResult["msg"];
                                break;
                        }
                    }
                    catch(Exception $ex){
                        $oLog->writeException('generictask: commentPost():: \t'. $ex->getMessage(). "\n");
                        echo "400";
                    }
                    break;
                
                case 6: //comment remove
                    try {
                        $taskResult= $postGenricTask->removePostComment($feedid,$loggedInId,$commentId);
                        switch ($taskResult["err_code"]) {
                            case 200:                                
                                echo $taskResult["err_code"];
                                break;
                            default:
                                echo $taskResult["msg"];
                                break;
                        }
                    }
                    catch(Exception $ex){
                        $oLog->writeException('generictask: commentPost():: \t'. $ex->getMessage(). "\n");
                        echo "400";
                    }
                    break;
                 case 8: //share
                        //get post to share details for sharer popup
                    try {
                        $objReadNewsFeed= new ReadFeedAction();
                        $sharedPostsnapshotdetails=$objReadNewsFeed->getPostSnapshotDetails($feedid,$posttypeId,$postparentid,$clusteredId);
                        if(is_array($sharedPostsnapshotdetails) && count($sharedPostsnapshotdetails)>0){
                            echo getPostSnapshot($sharedPostsnapshotdetails);
                        }
                    }
                    catch(Exception $ex){
                        $oLog->writeException('generictask: commentPost():: \t'. $ex->getMessage(). "\n");
                        echo "400";
                    }
                    break;
                    
            }
        }
        else{
            
        }
    
    }
    catch(Exception $ex){
            
    } 
}


function getPostCommentsHtml($feedComments,$feedid){
    $feedSection="";
    $commentUserDetail= new UserDetails();
    if(isset($feedComments) && is_array($feedComments) && count($feedComments)>0){
        for($pccount=0;$pccount<count($feedComments);$pccount++){
            $postCommentslist=$feedComments[$pccount];
        $commentUserDetail->getUserDetails($postCommentslist["User_ID"]);
        $postCommentslist["Post_Comment"]=getSmileyDataForSave($postCommentslist["Post_Comment"]);
    		$feedSection.='<!--Comment-->
                <div class="status2">
    			    <div class="status_top">
    				   <div class="more_pic">
                            <!--
    					   <span><img src="images/ico/13.png"></span>
    					   <span style="margin-top:5px;">000 more...</span>
    					   <span style="margin-top:5px; margin-left:10px;">Pick 0</span>
                           -->
    					   <div class="clearfix"></div>
        					<table width="100%" border="0" cellpadding="5" cellspacing="5">
        					   <tbody>
                                    <tr>
        							     <td width="10%" rowspan="2">
        							         <div class="more_picture"><img src="'.$commentUserDetail->profilePicPath.'" width="40" /></div>
                                         </td>
        							     <td width="90%" class="box_home_heading" style="text-shadow:none !important">'.$postCommentslist["Full_Name"].'</td>
        						     </tr>
        						     <tr>
        							     <td class="box_txt">
                                            <span>'.$postCommentslist["Post_Comment"].'</span>
                                            <span style="margin-left:20px;"onclick="likeComment('.$postCommentslist["User_Comment_ID"].','.$feedid.');">Pick <span id="cmt_'.$postCommentslist["User_Comment_ID"].'">0</span></span>
                                         </td>
        						      </tr>
        					   </tbody>
                            </table>
    				</div>
    				<div class="clearfix"></div>
    			</div>
    		</div>';
        
        }
    }
    echo $feedSection;
}

function getPostSnapshot($feedSnapshot){
    if(isset($feedSnapshot) && is_array($feedSnapshot) && count($feedSnapshot)>0){
        $feedSnapshot=$feedSnapshot[0];
        $postUploads=array();
        if($feedSnapshot["Post_ID"]!=null && is_numeric(trim($feedSnapshot["Post_ID"])) && trim($feedSnapshot["Post_ID"])>0){
            $postFeeds= new ReadFeedAction();
            $postComments=$postFeeds->ReadPostComments($feedSnapshot["Post_ID"]);   
            $postUploads= $postFeeds->ReadPostUploads($feedSnapshot["Post_ID"]);
            //$postUploads=(isset($postUploads[0]))?$postUploads[0]:array();
        }
        $feedSnapshot["PostedContent"]=getSmileyDataForSave($feedSnapshot["PostedContent"]);
        $feedSection= '<div class="feed" style="text-align:left">
                <table width="100%" border="0">
                	<tbody>
                        <tr>
                                <td width="14%" valign="top"><div class="other_box"><img src="'.$feedSnapshot['Profile_Pic_Path'].'" width="60" /> </div></td>
                                <td width="71%" valign="top">
                                    <p class="box_home_heading">'.$feedSnapshot['Full_Name'].'</p><br>
                                    <p class="box_txt">
                                        '.$feedSnapshot["PostedContent"].'
                                    </p>';
                                    if(is_array($postUploads) && count($postUploads)>0){
                                        $mediacounts=0;
                                        foreach($postUploads as $postUpload){
                                            $mediacounts++;
                                            
                                            $feedSection.='<div style="float:left;padding:2px">';
                                            $feedSection.='<img src="'.$postUpload["Media_Path"].'" width="200px">';
                                            $feedSection.='</div>';
                                            if($mediacounts%2==0){
                                                //$feedSection.="<div></div>";
                                            }
                                        }
                                    }
                 $feedSection.='</td>
                         </tr>
                      </tbody>
                </table>
                </div>'; 
    }
    else{
        $feedSection="no record found";
    }
return $feedSection;   
}

?>